<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;

use herosphp\core\Loader;


class MessageAction extends CommonAction
{

    public function C_start()
    {
        parent::C_start();


    }

    protected $serviceBean = "message.message.service";


    public function index(HttpRequest $request)
    {
        $messageService = Beans::get($this->serviceBean);

        $page = $request->getParameter('page');
        $pagesize = $request->getParameter('pagesize');

        if ($page <= 0) $page = 1;
        if ($pagesize <= 0) $pagesize = 15;
        $this->page=$page;
        $this->pagesize=$pagesize;

        $search_arr = array(
            "select_mix_option1" => $request->getParameter('select_mix_option1'),
            "select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),
            "select_mix_option2" => $request->getParameter('select_mix_option2'),
            "start_time" => $request->getParameter('start_time'),
            "end_time" => $request->getParameter('end_time'),
            "status" => $request->getParameter('status'),


        );
        if( $request -> getParameter('export') == 1){
            $page = 1;
            $pagesize = 2147483648;
            $ids = $request -> getParameter('ids');
            if(is_array($ids)){
                $str = "(0";
                foreach ($ids as $value){
                    $str .= ",".$value;
                }
                $str .=")";
                $search_arr['id']=$str;
            }
        }

        $arr = $messageService->search($pagesize, $page, $search_arr);

        $items = $arr['items'];

        $itemsLen = count($items);

        for ($i = 0; $i < $itemsLen; $i++) {

            $items[$i]['status'] = $messageService->getMessageStatusText($items[$i]['status']);
        }

        if( $request->getParameter('export') == 1 ){
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["member_username"];
                $tmp[] = $v["tmpl_name_zh"];
                $tmp[] = $v["message_content"];
                $tmp[] = $v["status"];
                $tmp[] = $v["sendtime"];
                $tmp[] = $v["group_name"];
                $tmp[] = $v["status"] == 1 ? "启用" : "禁用";
                $tmp[] = long2ip($v["last_login_ip"]);
                $excel[] = $tmp;
            }
            $header = array('会员号','业务类型','内容','状态','通知时间');
            phpexcel("消息中心",$header,$excel);
            exit();
        }
        $this->generatePageMenu($arr['total']);

        $this->assign('search_arr', $search_arr);
        $this->assign('items', $items);
        $this->assign('search_arr', $search_arr);
        $this->assign("title", "消息中心");
        $this->assign('bread', array('网站设置',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
        $this->setView(strtolower($request->getAction()) . '/index');

    }



    //忽略消息
    public function do_ignore(HttpRequest $request)
    {
        $messageService = Beans::get($this->serviceBean);

        $messageModel = Loader::model('message');

        $ids = $request->getParameter('ids');

        if (empty($ids)) AjaxResult::ajaxResult('error', COM_ERR_MSG);
        if (!is_array($ids)) {

            $ids = array($ids);
        }

        $data = array('status' => $messageModel::STATUS_IS_HANDLED);

        if ($messageModel->updates($data, array('id' => array('$in' => $ids) ))) {
            $messageService->handleMessageArray(true);
            AjaxResult::ajaxSuccessResult();
        } else {
            AjaxResult::ajaxFailtureResult();
        }


    }


    //获取未处理消息
    public function getUnHandledMessage(HttpRequest $request)
    {
        $messageService = Beans::get($this->serviceBean);
        $messageArr = $messageService->handleMessageArray();
        if ($messageArr) {
            AjaxResult::ajaxResult('000', 'ok', $messageArr);
        } else {
            AjaxResult::ajaxResult('001', 'no', '');
        }
    }


}
